package org.example;

import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @author xhw
 */
public class test19 {
    public static void main(String[] args) {
        //创建一个对现象并初始化属性
        TestObject jsonObject = new TestObject(1, 1, true, Boolean.FALSE);

        // 使用Jackson进行对象序列化和反序列化
        ObjectMapper mapper = new ObjectMapper();

        // 使用try-with-resources优化代码，自动关闭资源
        try {
            // 对象序列化
            String serializedObject = mapper.writeValueAsString(jsonObject);
            System.out.println("序列化后的对象：" + serializedObject);
            // 对象反序列化
            TestObject deserializedObject = mapper.readValue(serializedObject, TestObject.class);
            // 反序列化输出
            System.out.println("反序列化后的对象属性值：");
            System.out.println("intValue: " + deserializedObject.getIntValue());
            System.out.println("integerValue: " + deserializedObject.getIntegerValue());
            System.out.println("booleanValue: " + deserializedObject.isBooleanValue());
            System.out.println("booleanObject: " + deserializedObject.getBooleanObject());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Getter
    @Setter
    @NoArgsConstructor
    public static class TestObject {
        private int intValue;
        private Integer integerValue;
        private boolean booleanValue;
        private Boolean booleanObject;

        public TestObject(int intValue, Integer integerValue, boolean booleanValue, Boolean booleanObject) {
            this.intValue = intValue;
            this.integerValue = integerValue;
            this.booleanValue = booleanValue;
            this.booleanObject = booleanObject;
        }
    }

}
